///////////////////////////////////////////////////////
/// Script: EssMiner.java /////////////////////////////
/// Author: Zeldo /////////////////////////////////////
/// Base Script by MrPanda ////////////////////////////
/// Images by Master of RSBot /////////////////////////
/// Version: 2.6 //////////////////////////////////////
/// Directions: ///////////////////////////////////////
/// 1. Save in Scripts folder of RSBot ////////////////
///    as EssMiner.java ///////////////////////////////
/// 2. Compile in RSBot (Compile->Compile Scripts) ////
///////////////////////////////////////////////////////
/// If you edit this script and redistribute it ///////
///		please give credit where due, thanks! /////////
///////////////////////////////////////////////////////

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;
import java.util.Properties;

import com.speljohan.rsbot.bot.Bot;
import com.speljohan.rsbot.event.listeners.PaintListener;
import com.speljohan.rsbot.script.Calculations;
import com.speljohan.rsbot.script.Script;
import com.speljohan.rsbot.script.wrappers.RSInterface;
import com.speljohan.rsbot.script.wrappers.RSItem;
import com.speljohan.rsbot.script.wrappers.RSItemTile;
import com.speljohan.rsbot.script.wrappers.RSNPC;
import com.speljohan.rsbot.script.wrappers.RSObject;
import com.speljohan.rsbot.script.wrappers.RSTile;

public class EssMiner extends Script implements PaintListener {

	public double getVersion() {
		return (2.6);
	}

	public String getName() {
		return ("Rune|Pure Ess Miner");
	}

	public String getAuthor() {
		return ("Zeldo");
	}

	public String getScriptCategory() {
		return ("Mining");
	}

	public String getScriptDescription() {
		settingsExist(settingsFilename);
		getSetting(settingsFilename);
		String html = "";
		html += "<html>";
		html += "<body>";
		html += "<font size=\"2\">";
		html += "" + getName() + " v" + getVersion() + "<br>";
		html += "Author: " + getAuthor() + "<br>";
		html += "Images by: Master of RSBot<br><br>";
		html += "Start anywhere on the path to the Varrock ess mine with pick equiped/holding";
		html += "<br><br>0: Off 	1: Black	2: Red	3: White	4: Grey	5: Blue	6: Green	7: Yellow	8: Gold	9: Chocolate	10: Brown	11: Orange	12: Pink";
		html += "<br>BackColor 1 Color: <input type=\"text\" name=\"bgOneColor\" value=\"" + settingBgOneColor + "\"><br>";
		html += "<br>BackColor 2 Color: <input type=\"text\" name=\"bgTwoColor\" value=\"" + settingBgTwoColor + "\"><br>";
		html += "<br>Text Color: <input type=\"text\" name=\"txtColor\" value=\"" + settingTextColor + "\"><br><br>";
		html += "<br>Use Anti-Ban? <input type=\"checkbox\" name=\"useAntiBan\" checked=\"true\" value=\"true\">";
		html += "<br><b>Version 2.6</b><br>&nbsp;- Added Varrock Upstairs Fix and Failsafe";
		html += "<br><b>Version 2.5</b><br>&nbsp;- Added Prices by request";
		html += "<br><b>Version 2.4</b><br>&nbsp;- Made it so it trys to keep going after a error<br>&nbsp;- Redid Failsafe";
		html += "<br><b>Version 2.3</b><br>&nbsp;- Damn start-up bug fixed yet again. ";
		html += "<br><b>Version 2.2</b><br>&nbsp;- Fixed Start-Up bug again <br>&nbsp;- Added FailSafe, it shuts down if it can't get to the bank or mine";
		html += "<br><b>Version 2.1</b><br>&nbsp;- Added a update checker<br>&nbsp;- Added option to turn off antiBan (NOT RECOMMENDED)";
		html += "<br><b>Version 2.0</b><br>&nbsp;- Uses diffrent bank booths<br>&nbsp;- Dosen't have to be right next to Aub to Right-Click->Teleport<br>&nbsp;- Dosen't stop to teleport (Trys to while walking)<br>&nbsp;- Delayed PathChecking, was going too fast<br>&nbsp;- Fixed that Weclome Screen Bug";
		html += "<br><b>Version 1.9</b><br>&nbsp;- You don't stop before walking to the next spot now :)";
		html += "<br><b>Version 1.8</b><br>&nbsp;- Added AntiBan :D<br>&nbsp;- Cleaned up some code and recoded a few things to work faster";
		html += "<br><b>Version 1.7</b><br>&nbsp;- Added settings saving :)<br>&nbsp;- Changed ExpPerEss(wtf was I thinking?) to EssToLevel<br>&nbsp;- Moved GUI to the right side<br>&nbsp;- Added the option to turn off each part of the GUI";
		html += "<br><b>Version 1.6</b><br>&nbsp;- Changed the way it calculates EssPerHour to make it more accurate (This is more accurate the longer it runs)<br>&nbsp;- Added Custom GUI Colors";
		html += "<br><b>Version 1.5</b><br>&nbsp;- Added Ess Per Hour<br> - Added Offset for people who use the on-screen PM's<br>&nbsp;- Added a GUI around the info text and changed the color";
		html += "<br><b>Version 1.4</b><br>&nbsp;- Fixed a 1.3 Start-Up Bug";
		html += "<br><b>Version 1.3</b><br>&nbsp;- Added Alot More GUI Info <br>&nbsp;- Fixed Ess Counter to work in all situations";
		html += "<br><b>Version 1.2</b><br>&nbsp;- Fixed Randomized Paths<br>&nbsp;- Moved/Added more info to the GUI<br>";
		html += "</font>";
		html += "</body>";
		html += "</html>";

		return (html);
	}
	private boolean usePaint = false;
	public boolean activateCondition() {
		return RSInterface.getInterface(WELCOME_SCREEN_ID).isValid() && RSInterface.getInterface(WELCOME_SCREEN_ID).getChild(WELCOME_SCREEN_BUTTON_PLAY).getAbsoluteX() > 30;
	}

	public boolean onStart(Map<String, String> args) {
	
		try {
			backColorOne = bgColors[Integer.parseInt(args.get("bgOneColor"))];
			backColorTwo = bgColors[Integer.parseInt(args.get("bgTwoColor"))];
			textColor = txtColors[Integer.parseInt(args.get("txtColor"))];
			useAntiBan = args.get("useAntiBan") != null ? true : false;
		} catch (Exception e) {
			log("Error: Invalid Color Numbers!");
		}

		try {
			settingBgOneColor = args.get("bgOneColor").toString();
		} catch (NumberFormatException e) {
			log("Setting Error:  BgOneColor");
		}

		try {
			settingBgTwoColor = args.get("bgTwoColor").toString();
		} catch (NumberFormatException e) {
			log("Setting Error:  BgTwoColor");
		}

		try {
			settingTextColor = args.get("txtColor").toString();
		} catch (NumberFormatException e) {
			log("Setting Error:  txtColor");
		}

		usePaint = true;

		setSetting(settingsFilename);
		checkUpdate();
		getPrice();
		return (true);
	}

	public void onFinish() {
		Bot.getEventManager().removeListener(PaintListener.class, this);
		usePaint = false;
	}

	int failCount = 0;

	public int loop() {
		if (!isLoggedIn() || isWelcomeButton()) {
			return random(1000, 1100);
		}
		try {
			if (getMyPlayer().isMoving()) {
				if (getInventoryCount() != 28 && atShop()) {
					if (teleportToCave()) {
						return random(500, 800);
					}
				}
				if (System.currentTimeMillis() - lastMoveCheck >= 5000) {
					lastMoveCheck = System.currentTimeMillis();
				} else {
					return random(150, 200);
				}
			}
			if (getPlane() == 1) {
				wait(random(1000, 5000));
				climbObject("Climb-down", 24353);
			}
			if (checkTeleporting()) {
				return random(2800, 2900);
			}
			if (checkWalkingToBankBooth()) {
				return random(500, 600);
			}
			if (checkUsingPortal()) {
				return random(300, 400);
			}
			if (checkClickedRock()) {
				return antiBan();
			}
			if (checkWalkingToPortal()) {
				return random(300, 350);
			}
			if (checkAttack()) {
				return random(800, 1000);
			}
			if (getMyPlayer().getAnimation() != -1) {
				antiBan();
				return random(3000, 5000);
			}
			if (checkVarrockUpstairs()) {
				return random(800,1000);
			}
			int result;
			result = 20000;
			if (getInventoryCount() != 28) {
				result = doMine();
			} else {
				result = doBank();
			}
			if (result == 20000) {
				return random(1000,1200);
			}
			if (result != -1) {
				failCount = 0;
				return result;
			}
			log("walkBack");
			if (walkBack()) {
				failCount = 0;
				return random(500, 800);
			}
			failCount++;
			if (failCount >= 10 && failCount < 30) {
				log("homeTele");
				failCount = 0;
				homeTele();
				return random(2000, 3000);
			}
		} catch (Exception e) {
			log("Some type of error happened, prolly that damn welcome screen. keeping on task.");
			failCount++;
		}
		if (failCount >= 30) {
			log("Failsafe kicked in, loggin you out and killing the script");
			logout();
			stopAllScripts();
		}
		return random(1000, 2000);
		/*
		 * log("checkLost"); if(checkLost()) { return random(2000, 3000); }
		 * return random(100, 200);
		 */
	}

	public void onRepaint(Graphics g) {
	    if (usePaint) {
		g.setColor(Color.RED);
		long hours = 0, minutes = 0, seconds = 0;
		long time;

		if (startTime == 0) {
			startTime = System.currentTimeMillis();
		}
		if (startExp == 0) {
			startExp = skills.getCurrentSkillExp(STAT_MINING);
		}
		if (startLevel == 0) {
			startLevel = skills.getCurrentSkillLevel(STAT_MINING);
		}

		time = System.currentTimeMillis() - startTime;
		seconds = time / 1000;
		if (seconds >= 60) {
			minutes = seconds / 60;
			seconds -= minutes * 60;
		}

		if (minutes >= 60) {
			hours = minutes / 60;
			minutes -= hours * 60;
		}
		if (skills.getCurrentSkillExp(STAT_MINING) > oldExp) {
			minedEss++;
			oldExp = skills.getCurrentSkillExp(STAT_MINING);
			gainExp = skills.getCurrentSkillExp(STAT_MINING) - startExp;
			gainLevel = skills.getCurrentSkillLevel(STAT_MINING) - startLevel;
		}

		//if (System.currentTimeMillis() - oldAvg >= 60000) {
		//	oldAvg = System.currentTimeMillis();
		essPerHour = time / 1000;
		essPerHour = minedEss / essPerHour;
		essPerHour = essPerHour * 60;
		essPerHour = essPerHour * 60;
		//essPerHour = round(essPerHour, 2);
		//This new way is alot more accurate since Ess involves ALOT of 
		// walking the old way was very inaccurate... :\
		//essPerHour = (minedEss - minedEss2) * 60;
		//minedEss2 = minedEss;
		//}
		g.setColor(backColorOne);
		g.fill3DRect(260, topText-15, 250, 147, true);
		g.setColor(backColorTwo);
		g.fill3DRect(263, topText-13, 245, 142, true);
		g.setColor(textColor);

		g.drawString("Ess: " + minedEss, 265, topText);
		g.drawString("Loads: " + loads, 265, topText + 18);
		g.drawString("Ess To Level: " + skills.getXPToNextLevel(STAT_MINING) / expPerEss, 265, topText + 36);
		g.drawString("Levels Gained: " + gainLevel, 265, topText + 54);
		g.drawString("Exp Gained: " + gainExp, 265, topText + 72);
		g.drawString("Ess Per Hour: " + essPerHour, 265, topText + 90);
		g.drawString("Run Time: " + hours + " Hrs " + minutes + " Mins "
				+ seconds + " Secs", 265, topText + 108);
		g.drawString("Profit Pure/Rune: " + (minedEss * pricePureEss / 1000) + "k/" + (minedEss * priceRuneEss / 1000) + "k", 265, topText + 126);
	    }
	}

	int loads = 0;

	private void climbObject(String s, int i) {
		RSObject rso = findObject(i);
		int angle = getAngleToCoordinates(rso.getLocation().getX(), rso.getLocation().getY());
		int angleChange = angle + random(0, 120) - random(0, 120);
		setCameraRotation(angleChange);
		if (atObject(rso, s)) {
		}
	}

	public boolean walkBack() {
		return dpWalkPath(randomizePath(walkBackPath2, 3, 3));
	}

	public void homeTele() {
		openTab(Script.TAB_MAGIC);
		castSpell(1);
		failCount = 30;
	}

	public boolean checkAttack() {
		if (getMyPlayer().isInCombat()) {
			if (inMine()) {
				usePortal();
				return true;
			} else if (atBank()) {
				bankToShop();
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

	boolean clickedRock = false;

	public boolean checkClickedRock() {
		if (clickedRock) {
			clickedRock = false;
			return true;
		}
		return false;
	}

	public int doMine() {
		if (inCave()) {
			if (inMine()) {
				

				RSItemTile itemTile = getGroundItemByID(pickHeads);
				int foundGear = 0;
				if (getInventoryCount(picks) != 0 || equipmentContains(picks)) {
					foundGear = 1;
				}
				if (getInventoryCount(picks) == 0 && itemTile != null && pickUpHead < 3 && foundGear == 0) {
					if (isInventoryFull()) {
						if (getInventoryCount(7936) > 0) {
							RSItem pureEss = new RSItem(7936, 1);
							atInventoryItem(pureEss.getID(), "drop");
						} else {
							RSItem runeEss = new RSItem(1436, 1);
							atInventoryItem(runeEss.getID(), "drop");
						}
						return random(1000, 2000);
					}
					atTile(itemTile, "take");
					log("Giving it 3 chances to pick up that head...");
					pickUpHead++;
					return random(1000,2000);
				} 

				if (pickUpHead >= 3) {
					log("Well, FU axehandler...");
					logout();
					stopAllScripts();
				}
				clickedRock = clickRock();
				if (clickedRock) {
					return random(500, 800);
				} else {
					int result = distanceToByRockTile();
					if (result == -1) {
						return -1;
					}
					if (result > 0) {
						if (walkToRock()) {
							return random(500, 800);
						} else {
							log("E-1");
							return -1;
						}
					} else {
						// It can't find the rock to click! Move the Camera!!

						int angle = getCameraAngle() + random(-90, 90);
						if (angle < 0) {
							angle = 0;
						}
						if (angle > 359) {
							angle = 0;
						}

						setCameraRotation(angle);
						return random(500, 800);
					}
				}
			} else {
				if (centerToMine()) {
					return random(500, 800);
				} else {
					log("E-2");
					return -1;
				}
			}
		} else {
			if (atShop()) {
				if (openDoor()) {
					return random(500, 800);
				}
				if (teleportToCave()) {
					return random(500, 800);
				} else {
					if (walkIntoShop()) {
						return random(500, 800);
					} else {
						log("E-3");
						if (atShop()) {
							log("atshop true");
						} else {
							log("atshop false");
						}
						return -1;
					}
				}
			} else {
				if (bankToShop()) {
					return random(500, 800);
				} else {
					log("E-8");
					return -1;
				}
			}
		}
	}



	public int doBank() {
		if (inCave()) {
			if (inMine()) {
				setRun(true);
				if (usePortal()) {
					return random(700, 900);
				} else {
					if (walkToRock()) {
						return random(500, 800);
					} else {
						log("E-4");
						return -1;
					}
				}
			} else {
				if (centerToMine()) {
					return random(500, 800);
				} else {
					log("E-5");
					return -1;
				}
			}
		} else {
			if (atBank()) {
				if (RSInterface.getInterface(INTERFACE_BANK).isValid()) {
					bank.depositAllExcept(1275, 1265, 1269, 1267, 1273, 1271);
					loads++;
					return random(1000, 2000);
				} else {
					if (distanceTo(bankBoothTile) > 1) {
						if (walkToBankBooth()) {
							return random(500, 800);
						} else {
							return -1;
						}
					}
					RSObject object = findObject(bankBoothID);
					if (atObject(object, "uickly")) {
						return random(500, 800);
					} else {
						if (walkToBankBooth()) {
							return random(500, 800);
						} else {
							log("E-6");
							return -1;
						}
					}
				}
			} else {
				if (walkToBankBooth()) {
					return random(500, 800);
				} else {
					if (shopToBank()) {
						return random(500, 800);
					} else {
						log("E-7");
						return -1;
					}
				}
			}
		}
	}

	public boolean walkIntoShop() {
		return dpWalkTile(shopTile);
	}

	boolean walkingToBankBooth = false;

	public boolean walkToBankBooth() {
		if (dpWalkTile(bankBoothTile)) {
			walkingToBankBooth = true;
			return true;
		} else {
			return false;
		}
	}

	public boolean checkWalkingToBankBooth() {
		if (walkingToBankBooth) {
			walkingToBankBooth = false;
			return true;
		} else {
			return false;
		}
	}

	public boolean inCenter() {
		return distanceTo(centerTile) < 10;
	}

	public boolean inCave() {
		return distanceTo(centerTile) < 50;
	}

	public boolean atBank() {
		return distanceTo(bankTile[random(0,5)]) < 12;
	}

	public boolean atShop() {
		return distanceTo(shopTile) < 7;
	}

	public boolean inMine() {
		return inCave() && !inCenter();
	}

	boolean usingPortal = false;

	public boolean usePortal() {
		RSObject object = findObject(portalID);
		if (atObject(object, "Use")) {
			usingPortal = true;
			return true;
		} else {
			if (dpWalkTile(nePortalTile)) {
			} else if (dpWalkTile(nwPortalTile)) {
			} else if (dpWalkTile(sePortalTile)) {
			} else if (dpWalkTile(swPortalTile)) {
			} else {
				return false;
			}
			walkingToPortal = true;
			return true;
		}
	}

	boolean walkingToPortal = false;

	public boolean checkWalkingToPortal() {
		if (walkingToPortal) {
			walkingToPortal = false;
			return true;
		} else {
			return false;
		}
	}

	public boolean checkUsingPortal() {
		if (usingPortal) {
			usingPortal = false;
			return true;
		} else {
			return false;
		}
	}

	public boolean openDoor() {
		RSObject object = findObject(24381);
		if (object == null || object.getLocation().getX() != 3253) {
			return false;
		}
		Point point = new Point((int) Calculations.tileToScreen(
				object.getLocation()).getX(), (int) Calculations.tileToScreen(
						object.getLocation()).getY()
						- random(18, 20));
		moveMouse(point);
		clickMouse(point, true);
		return true;
	}

	boolean teleporting = false;
	public boolean teleportToCave() {
		RSNPC npc = getNearestFreeNPCByName("Aubury");
		Point D = npc.getScreenLocation();
		D.setLocation(-1,-1);
		if (npc.getScreenLocation() == D) {
			return false;
		}
		if (atNPC(npc, "Teleport")) {
			teleporting = true;
			return true;
		} else {
			return false;
		}
	}

	public boolean dpWalkPath(RSTile[] path) {
		for (int i = path.length - 1; i >= 0; i--) {
			if (dpWalkTile(path[i])) {
				return true;
			}
		}
		return false;
	}

	public boolean dpWalkTile(RSTile tile) {
		if (distanceTo(tile) <= 16) {
			walkTileMM(tile);
			return true;
		} else {
			return false;
		}
	}

	public boolean shopToBank() {
		if (openDoor()) {
			return true;
		}
		return dpWalkPath(randomizePath(shopToBankPath2, 3, 3));
	}

	public boolean bankToShop() {
		return dpWalkPath(randomizePath(bankToShopPath2, 3, 3));
	}

	public boolean centerToMine() {
		return dpWalkPath(randomizePath(centerToMinePath2, 3, 3));
	}

	public boolean dpClickRockTile(RSTile tile) {
		return atTile(tile, "ine");
	}

	public boolean clickNERockTile() {
		return dpClickRockTile(neRockTile);
	}

	public boolean clickNWRockTile() {
		return dpClickRockTile(nwRockTile);
	}

	public boolean clickSERockTile() {
		return dpClickRockTile(seRockTile);
	}

	public boolean clickSWRockTile() {
		return dpClickRockTile(swRockTile);
	}

	public boolean walkToRock() {
		if (dpWalkTile(neByRockTile)) {
			return true;
		} else if (dpWalkTile(nwByRockTile)) {
			return true;
		} else if (dpWalkTile(seByRockTile)) {
			return true;
		} else if (dpWalkTile(swByRockTile)) {
			return true;
		} else {
			return false;
		}
	}

	public boolean inMineTile(RSTile tile) {
		return distanceTo(tile) < 15;
	}

	public boolean inNEMine() {
		return inMineTile(neRockTile);
	}

	public boolean inNWMine() {
		return inMineTile(nwRockTile);
	}

	public boolean inSEMine() {
		return inMineTile(seRockTile);
	}

	public boolean inSWMine() {
		return inMineTile(swRockTile);
	}

	public int distanceToByRockTile() {
		if (inNEMine()) {
			return distanceTo(neByRockTile);
		}
		if (inSEMine()) {
			return distanceTo(seByRockTile);
		}
		if (inNWMine()) {
			return distanceTo(nwByRockTile);
		}
		if (inSWMine()) {
			return distanceTo(swByRockTile);
		}
		return -1;
	}
	public boolean checkVarrockUpstairs() {
		//return false;
		int floor = getPlane();
		if (floor == varFloor) {
			RSObject obj = findObject(varStairs);
			if(obj != null){
				atObject(obj, "Climb");
				return true;
			}
			return true;
		}
		return false;
	}

	public boolean clickRock() {
		if (inNEMine()) {
			return clickNERockTile();
		}
		if (inNWMine()) {
			return clickNWRockTile();
		}
		if (inSEMine()) {
			return clickSERockTile();
		}
		if (inSWMine()) {
			return clickSWRockTile();
		}
		return false;
	}

	public boolean checkTeleporting() {
		if (teleporting) {
			teleporting = false;
			return true;
		}
		return false;
	}

	//////////////////////////////////////////////
	////// AntiBan ///////////////////////////////
	////// moveMouse is most often called ////////
	////// because that's what a human ///////////
	////// would do most /////////////////////////
	//////////////////////////////////////////////
	public int antiBan() {
		if (useAntiBan == false) { return random(200,400); }
		int antiBanType = random(1,8);
		switch (antiBanType) {
		case 1: return moveMouse();
		case 2:
			if (getCurrentTab() != TAB_STATS) {
				openTab(TAB_STATS);
			}
			// 687 241 is about the center of the skill
			// add randomness, after all we are human
			// we don't hit the same spot every time ;)
			int x2 = 661;
			int y2 = 228;
			moveMouse(x2, y2, 40, 25);
			log("AntiBan Debug: Stats Check (" + x2 + "," + y2 + ")");
			return random(5000, 8000);
		case 3: return moveMouse();
		case 4:
			antiBanType = random(0,13);
			if (antiBanType == Script.TAB_SUMMONING) { antiBanType = random(0,6); } //FailSafe for free players
			openTab(antiBanType);
			log("AntiBan Debug: Random Tab (" + tabNames[antiBanType] + " Tab)");
			return random(2000,3000);
		case 5: return moveMouse();
		case 6:
			if (random(1, 4) == 2) {
				int angle = getCameraAngle() + random(-90, 90);
				if (angle < 0) {
					angle = 0;
				}
				if (angle > 359) {
					angle = 0;
				}
				log("AntiBan Debug: Cam Change (" + angle + ")");
				setCameraRotation(angle);
				return random(500, 750);
			}
			log("AntiBan Debug: Cam Change (Didn't Move)");
			return random(500, 750);
		case 7: return moveMouse();
		case 8:
			log("AntiBan Debug: Random Return");
			//Don't need to look like we are
			//spazzing and messing with stuff
			//every second we can!
			return random(500,1500);
		default:
			log("AntiBan Debug: AntiBan Error! Unknown Case");
		}
		return random(1000,2000);
	}

	public void checkUpdate() {
		try {
			log("[Update Checker] Checking for updates...");
			URL url = new URL("http://zeldo.net/update.php");
			BufferedReader br = new BufferedReader(new InputStreamReader(
					new BufferedInputStream(url.openConnection().getInputStream())));
			double ver = Double.parseDouble(br.readLine().trim());
			br.close();
			log("[Update Checker] Current Version: " + ver);
			log("[Update Checker] Your Version: " + getVersion());
			if (getVersion() >= ver) {
				log("[Update Checker] You are up to date!");
				log("[Update Checker] Fourm Topic: http://www.rsbot.org/mybb/thread-7158.html");
			} else {
				log("[Update Checker] You need to update! http://zeldo.net/EssMiner.java" + ver);
				log("[Update Checker] Or you can visit the fourm! http://www.rsbot.org/mybb/thread-7158.html");
				//log("[Update Checker] If you have copy issues with the other link download it here http://zeldo.net/EssMiner.java");
			}
		} catch (IOException e) {
			log("Update Error, oh well... Running Script.");
		}  
	}
	public void getPrice() {
		try {
			log("[Price] Getting Price for Pure Ess");
			pricePureEss = getMarketPriceOfItem(7936);
			log("[Price] Pure Ess Price: " + pricePureEss);
			log("[Price] Getting Price for Rune Ess");
			priceRuneEss = getMarketPriceOfItem(1436);
			log("[Price] Rune Ess Price: " + priceRuneEss);
		} catch (Exception e) {
			log("[Price] Error getting price");
		}  
	}

	public int moveMouse() {
		if (System.currentTimeMillis() - lastAntiBan > 6000) {
			int x = random(0, 750);
			int y = random(0, 500);
			moveMouse(0, 0, x, y);
			log("AntiBan Debug: Mouse Move (" + x + "," + y + ")");
			return random(1000, 1500);
		}
		return random(1000, 1500);
	}
	public void getSetting(String filename) {
		try {
			Properties p = new Properties();
			p.load(new FileInputStream(filename));
			settingBgOneColor = p.getProperty("bgColorOne");
			settingBgTwoColor=p.getProperty("bgColorTwo");
			settingTextColor=p.getProperty("txtColor");
			return;
		} catch (IOException ioe) {
			//System.out.print("loadSettings(String) error: " + ioe);
			return;
		}
	}
	@SuppressWarnings("deprecation")
	public void setSetting(String filename) {
		try {
			Properties p = new Properties();
			p.setProperty("bgColorOne", settingBgOneColor);
			p.setProperty("bgColorTwo", settingBgTwoColor);
			p.setProperty("txtColor", settingTextColor);
			p.save(new FileOutputStream(filename), filename);
			return;
		} catch (IOException ioe) {
			System.out.print("setSettings(String) error: " + ioe);
			return;
		}
	}

	public boolean settingsExist(String filename) {
		File settingsFile = new File(filename);
		return settingsFile.exists();
	}
	public boolean isWelcomeButton()
	{        
		RSInterface welcomeInterface = RSInterface.getInterface(378);
		if(welcomeInterface.getChild(45).getAbsoluteX() > 20 || (!welcomeInterface.getChild(117).getText().equals("10.1120.190") && !welcomeInterface.getChild(117).getText().equals(""))) { 
			log("Not crashing this time!!");
			return true;    
		} else {
			return false;
		}    
	}
	public int getMarketPriceOfItem(int id) {
		String pageSource = "";
		int begin = 0;
		int end = 0;
		try {
			URL theUrl = new URL("http://itemdb-rs.runescape.com/viewitem.ws?obj=" + id);
			URLConnection theUrlConnection = theUrl.openConnection();
			BufferedReader in = new BufferedReader(new InputStreamReader(theUrlConnection.getInputStream()));
			String inputLine = "";
			while ((inputLine = in.readLine()) != null)
				pageSource += inputLine;
			in.close();
			pageSource = pageSource.replaceAll("\n", "");
			pageSource = pageSource.replaceAll("\t", "");
			begin = pageSource.indexOf("<b>Market price:</b> ") + ("<b>Market price:</b> ").length();
			end = pageSource.indexOf("</span><span><b>Maximum price:</b>");
		} catch (Exception e) {
			System.out.println("[Price] Error getting price!");
		}
		return (int)new Integer(pageSource.substring(begin, end)).intValue();
	}

	public RSTile bankTile[]  = { new RSTile(3253, 3420), new RSTile(3251, 3420), new RSTile(3252, 3420), new RSTile(3254, 3420), new RSTile(3255, 3420), new RSTile(3256, 3420) };

	public RSTile shopTile = new RSTile(3253, 3401);

	public RSTile centerTile = new RSTile(2910, 4832);

	public RSTile nwPortalTile = new RSTile(2886, 4850);

	public RSTile swPortalTile = new RSTile(2890, 4813);

	public RSTile sePortalTile = new RSTile(2932, 4815);

	public RSTile nePortalTile = new RSTile(2931, 4854);

	public RSTile bankBoothTile = new RSTile(3253, 3420);

	public RSTile neRockTile = new RSTile(2927, 4849);

	public RSTile neByRockTile = new RSTile(2925, 4847);

	public RSTile nwRockTile = new RSTile(2893, 4848);

	public RSTile nwByRockTile = new RSTile(2895, 4846);

	public RSTile seRockTile = new RSTile(2927, 4816);

	public RSTile seByRockTile = new RSTile(2926, 4816);

	public RSTile swRockTile = new RSTile(2895, 4813);

	public RSTile swByRockTile = new RSTile(2897, 4811);

	public int bankBoothID = 11402;

	public int rockID = 2491;

	public int portalID = 2492;

	public int closedDoorID = 24381;

	public RSTile[] bankToShopPath2 = { new RSTile(3254, 3426),
			new RSTile(3260, 3416), new RSTile(3260, 3405),
			new RSTile(3253, 3399) };

	public RSTile[] shopToBankPath2 = { new RSTile(3253, 3398),
			new RSTile(3256, 3409), new RSTile(3260, 3420),
			new RSTile(3254, 3428) };

	public RSTile[] centerToMinePath2 = { new RSTile(2906, 4827),
			new RSTile(2901, 4817), new RSTile(2897, 4811) };

	public RSTile[] walkBackPath2 = { new RSTile(3222, 3219),
			new RSTile(3233, 3221), new RSTile(3244, 3225),
			new RSTile(3255, 3227), new RSTile(3259, 3238),
			new RSTile(3257, 3249), new RSTile(3252, 3259),
			new RSTile(3247, 3269), new RSTile(3241, 3279),
			new RSTile(3239, 3290), new RSTile(3239, 3301),
			new RSTile(3244, 3311), new RSTile(3251, 3320),
			new RSTile(3262, 3322), new RSTile(3270, 3330),
			new RSTile(3281, 3332), new RSTile(3286, 3342),
			new RSTile(3295, 3349), new RSTile(3299, 3360),
			new RSTile(3299, 3371), new RSTile(3296, 3382),
			new RSTile(3294, 3393), new RSTile(3293, 3404),
			new RSTile(3292, 3415), new RSTile(3283, 3422),
			new RSTile(3272, 3426), new RSTile(3261, 3427),
			new RSTile(3253, 3421) };

	Color bgColors[] = { new Color(0, 0, 0, 0), //Off
			new Color(0, 0, 0, 50), //Black
			new Color(255, 0, 0, 50), //Red
			new Color(255, 255, 255, 50), //White
			new Color(190, 190, 190, 50), //Grey
			new Color(0, 0, 255, 50), //Blue
			new Color(0, 255, 0, 50), //Green
			new Color(255, 255, 0, 50), //Yellow
			new Color(255, 215, 0, 50), //Gold
			new Color(210, 105, 30, 50), //Chocolate
			new Color(165, 42, 42, 50), //Brown
			new Color(255, 165, 0, 50), //orange
			new Color(255, 20, 147, 50) }; //pink

	Color txtColors[] = { new Color(0, 0, 0, 0), //Off
			new Color(0, 0, 0), //Black
			new Color(255, 0, 0), //Red
			new Color(255, 255, 255), //White
			new Color(190, 190, 190), //Grey
			new Color(0, 0, 255), //Blue
			new Color(0, 255, 0), //Green
			new Color(255, 255, 0), //Yellow
			new Color(255, 215, 0), //Gold
			new Color(210, 105, 30), //Chocolate
			new Color(165, 42, 42), //Brown
			new Color(255, 165, 0), //orange
			new Color(255, 20, 147) }; //pink

	private long minedEss = 0;
	private long startExp = skills.getCurrentSkillExp(STAT_MINING);
	private long gainExp = 0;
	private long oldExp = skills.getCurrentSkillExp(STAT_MINING);
	private int startLevel = skills.getCurrentSkillLevel(STAT_MINING);
	private int gainLevel = 0;
	private long startTime = System.currentTimeMillis();
	private double essPerHour = 0.0;
	private int topText = 207;
	Color backColorOne = bgColors[1];
	Color backColorTwo = bgColors[2];
	Color textColor = txtColors[5];
	private String settingsFilename = "EssMinerSettings.ini";
	private String settingBgOneColor = "1";
	private String settingBgTwoColor = "2";
	private String settingTextColor = "5";
	private int expPerEss = 5;
	private long lastAntiBan = System.currentTimeMillis();
	String[] tabNames = new String[]{ "Attack", "Stats", "Quests", "Inventory", "Equipment", "Prayer", "Magic", "Summoning", "Friends", "Ignore", "Clan Chat", "Options", "Controls", "Music", "Logout" };
	final static int WELCOME_SCREEN_ID = 378;
	final static int WELCOME_SCREEN_BUTTON_PLAY = 45;
	private long lastMoveCheck = 0;
	private boolean useAntiBan = true;
	private int priceRuneEss = 0;
	private int pricePureEss = 0;
	private int pickUpHead = 0;
	private static final int[] pickHeads = { 480, 482, 484, 486, 488, 490 };
	private static final int[] picks = new int[] { 1275, 1265, 1269, 1267, 1273,
		1271 };
	private static final int varStairs = 24349;
	private static final int varFloor = 1;
}